package com.FH.permission.org_role;

import com.FH.permission.sys_role.SysroleShowDTO;
import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by acer on 2016/8/3.
 */

@Stateless
public class OrgroleDAOImpl extends AccessDB implements OrgroleDAO {
    @Override
    public ResultDTO getOrgroleInfo(String search, String page, String size) {

        ResultDTO resultDTO = new ResultDTO();
        try{
            JSONObject jsonObject=new JSONObject(search);
            String sql_w ="";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);//将定义page、size的String类型转换成Integer类型
            Integer size_ok = Integer.parseInt(size);

            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok + 1) + "," + size + "";
            }


            if(jsonObject.has("sys_role_id")){//角色id
                sql_w += " AND sys_role_id ="+jsonObject.getString("sys_role_id");
            }
            if(jsonObject.has("org_role_id")){//角色id
                sql_w += " AND org_role_id ="+jsonObject.getString("org_role_id");
            }

            String sqlexit = "select count(1) from  org_role  where 1=1 ";
            String sqlexit_ok = sqlexit + sql_w;

            String existName = getReportingToOne(sqlexit_ok);//查询此数据是否存在

            List<String []> seresult = new ArrayList<String []>();//存储查询到的结果

            List<OrgroleShowDTO> result = new ArrayList<OrgroleShowDTO>();

            if(!existName.equals("0")){

                String sql="select o.org_role_id,o.sys_role_id,o.org_role_create,o.org_role_create_time,o.org_role_modify,o.org_role_modify_time" +
                        ",s.sys_role_name from org_role o,sys_role s where o.sys_role_id=s.sys_role_id ";

                seresult = this.getReportingTo(sql);
            }

            if(seresult.size()>0){
                for(int i=0;i<seresult.size();i++){
                    OrgroleShowDTO orgroleShowDTO=new OrgroleShowDTO();

                    orgroleShowDTO.setOrg_role_id(seresult.get(i)[1]);
                    orgroleShowDTO.setSys_role_id(seresult.get(i)[2]);
                    orgroleShowDTO.setOrg_role_create(seresult.get(i)[3]);
                    orgroleShowDTO.setOrg_role_create_time(seresult.get(i)[4]);
                    orgroleShowDTO.setOrg_role_modify(seresult.get(i)[5]);
                    orgroleShowDTO.setOrg_role_modify_time(seresult.get(i)[6]);
                    orgroleShowDTO.setSys_role_name(seresult.get(i)[7]);
                    result.add(orgroleShowDTO);
                }
                resultDTO.setValues(result);
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            }else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }


        }catch(Exception e){
            e.printStackTrace();
        }

        return resultDTO;
    }

    /**
     * 插入数据
     * @param orgrolePullDTO
     * @return
     */
    @Override
    public ResultDTO addOrgroleInfo(OrgrolePullDTO orgrolePullDTO) {

        int resultls=0;

        ResultDTO resultDTO=new ResultDTO();//创建返回对象
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String uuid= UUID.randomUUID().toString().trim().replaceAll("-", "");

        String Orgrole_insert="";
               Orgrole_insert="insert into org_role (org_role_id,sys_role_id,org_role_create,org_role_create_time,org_role_modify,org_role_modify_time)" +
                       "values('"+uuid+"','"+orgrolePullDTO.getSys_role_id()+"','"+orgrolePullDTO.getOrg_role_create()+"','"+df.format(new Date())+"'," +
                       "'"+orgrolePullDTO.getOrg_role_modify()+"','"+orgrolePullDTO.getOrg_role_modify_time()+"')";

        resultls= addReportingTo(Orgrole_insert);

        if(resultls!=0){
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.add_success);
        }else{
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.add_fail);
        }
        return resultDTO;
    }
}
